<?php
// +----------------------------------------------------------------------
// | LIKE [ JUST DO IT ]
// +----------------------------------------------------------------------
// | Author: Mr.hu <huhaiyang7788@163.com>
// +----------------------------------------------------------------------
// | DESC: 用户帐号密码登录，获取对应的token
// +----------------------------------------------------------------------

namespace api\controllers;



use api\service\Login;
use api\service\login\Admin;
use api\service\login\Weixin;

class LoginController extends BaseController
{
    const LOGIN_TYPE_ADMIN='admin';                 //后台登录方式
    const LOGIN_TYPE_WX='weixin';                   //微信登录方式


    public $loginService;                           //实现登录获取的token的service

    /**
     * 一步过掉token验证
     * @param $token
     * return bool
     * @ Mr.hu
     */
    public function validateToken($token)
    {
        return true;
    }


    /**
     * 用户登录并返回token
     * @param string $type
     * return token
     * @ Mr.hu
     */
    public function actionLogin($type='admin')
    {
        if($this->setLogin($type)){
            $token=$this->loginService->logIn($this->processed_data);
            if(false===$token){
                return $this->error(40002);
            }
        }else{
            return $this->error(10001);
        }
    }

    /**
     * 用户退出登录
     * @param string $type
     * return void
     * @ Mr.hu
     */
    public function actionLogOut($type='admin')
    {
        if($this->setLogin($type)){
            $result=$this->loginService->logOut($this->processed_data['token']);
            if(false===$result){
                return $this->error(10001);
            }
        }else{
            $this->error(10001);
        }
    }


    /**
     * 适配不同的login登录方式
     * @param string $type
     * return void
     * @ Mr.hu
     */
    private function setLogin($type='admin'){
        switch ($type){
            case self::LOGIN_TYPE_ADMIN:
                return  $this->setLoginService(new Admin());
            case self::LOGIN_TYPE_WX:
                return $this->setLoginService(new Weixin());
        }
    }

    /**
     * 设置不同的登录的service
     * @param Login $login
     * return void
     * @ Mr.hu
     */
    private function setLoginService(Login $login){
        $login->setTokenService(self::$tokenService);
        return $this->loginService=$login;
    }



}